package com.hxgis.oc.cma;

import cn.hutool.json.JSONObject;
import com.hxgis.oc.cma.util.CmaFileInfo;
import com.hxgis.oc.cma.util.CmaFileRequest;
import com.hxgis.oc.cma.util.CmaRequest;
import com.hxgis.oc.config.OConfig;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SwanRequest extends CmaFileRequest {
    public static final String DataCode = "RADA_SWAN_L3_MOSAIC";
    private static final String interfaceId = "getRadaFileByTimeRange";

    //Z_OTHE_RADAMOSAIC_20200718153000.bin.bz2
    public LocalDateTime parseTimeFromName(String fileName)
    {
        LocalDateTime localDateTime = null;
        String[] list = fileName.split("_");
        if(list.length > 3){
            String timeYYYYMMDDHHMMSS = list[3];
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            try
            {
                localDateTime = LocalDateTime.parse(timeYYYYMMDDHHMMSS,formatter);
            }catch (Exception ignored){
            }
        }

        return localDateTime;
    }

    @Override
    public String getDataTable() {
        return DataCode;
    }

    @Override
    public List<CmaFileInfo> query(LocalDateTime fromTime, LocalDateTime toTime)
    {
        LocalDateTime utc_st = fromTime;
        LocalDateTime utc_ed = toTime;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String fromTimeStr = formatter.format(utc_st);
        String toTimeStr = formatter.format(utc_ed);

        Map<String, Object> requestParams = new HashMap<>();
        requestParams.put("dataCode",DataCode);
        requestParams.put("timeRange", "[" + fromTimeStr + "," + toTimeStr + ")");
        List<JSONObject> list = CmaRequest.queryJsonData(requestParams,interfaceId);
        return toFileInfoList(list,name -> generateTimeStamp(name));
    }
}
